/**
 * 
 */
package ch.il07.converter.business.impl;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import org.junit.Before;
import org.junit.Test;

import ch.il07.converter.business.Converter;
import ch.il07.converter.business.MeasurandAction;
import ch.il07.converter.business.UnitAction;
import ch.il07.converter.persistence.exceptions.NoSuchMeasurandException;
import ch.il07.converter.persistence.exceptions.NoSuchUnitException;
import ch.il07.converter.persistence.exceptions.NoSuchUserException;
import ch.il07.converter.persistence.exceptions.PersistenceException;

/**
 * @author stupidsheep
 * 
 */
public class ConverterImplTest {

	private Converter c = null;
	private MeasurandAction ma;
	private UnitAction ua;

	/**
	 * @throws java.lang.Exception
	 */
	@Before
	public void setUp() throws Exception {
		// Directly call the implementation!
		c = new ConverterImpl();
		ma = new MeasurandActionImpl();
		ua = new UnitActionImpl();
	}

	/**
	 * Test method for
	 * {@link ch.il07.converter.business.impl.ConverterImpl#calculate(java.lang.String, java.lang.String, java.lang.String, java.lang.String, float)}
	 * .
	 * 
	 * @throws NoSuchMeasurandException
	 *             Check the testdatabase!
	 */
	@Test
	public void testCalculatePublicMeasurand() {
		ma.addPublicMeasurand("Gewichtseinheit", null, "Kilogramm", null);
		ua.addPublicUnit("Gewichtseinheit", "Gramm", 0.001, 0.0, null);
		try {
			Double result;
			result = c.calculate(null, "Gewichtseinheit", "Kilogramm", "Gramm",
					2D);
			assertEquals(2000.00, result, 0.000001);
			ma.deletePublicMeasurand("Gewichtseinheit");
		} catch (NoSuchMeasurandException e) {
			fail(e.getMessage());
		} catch (NoSuchUserException e) {
			fail(e.getMessage());
		} catch (NoSuchUnitException e) {
			fail(e.getMessage());
		}
	}

	// calculate erhält:
	// From: Kilometer pro Stunde
	// To: Meter pro Sekunde
	// Measurand: Geschwindigkeit
	// Amount: 1.0
	@Test
	public void calculateSth() {
		try {
			Double d = c.calculate(null, "Geschwindigkeit", "Kilometer pro Stunde",
					"Meter pro Sekunde", 1.0D);
			System.out.println("Resultat: " + d);
		} catch (PersistenceException e) {
			fail(e.getMessage());
			e.printStackTrace();
		}

	}
}